package sk.vrto.web.views;

import com.vaadin.data.Validator;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;
import sk.vrto.service.dao.ContactRepository;
import sk.vrto.web.presenters.NewContactPresenter;

import java.util.regex.Pattern;

public class NewContactView extends FormLayout {

    transient private final NewContactPresenter presenter;

    protected TextField name;
    protected TextField mail;
    protected static Validator emailValidator;
    protected static Validator notEmptyStringValidator;

    static {
        emailValidator = new Validator() {

            private Pattern pattern = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

            @Override
            public void validate(Object value) throws InvalidValueException {
                if (!isValid(value)) {
                    throw new InvalidValueException("Not a valid e-mail");
                }
            }

            @Override
            public boolean isValid(Object value) {
                if (!(value instanceof String)) {
                    return false;
                }
                String val = (String) value;
                return pattern.matcher(val).matches();
            }
        };

        notEmptyStringValidator = new Validator() {
            @Override
            public void validate(Object value) throws InvalidValueException {
                if (!isValid(value)) {
                    throw new InvalidValueException("Value mustn't be empty");
                }
            }

            @Override
            public boolean isValid(Object value) {
                if (!(value instanceof String)) {
                    return false;
                }
                String val = (String) value;
                return !val.isEmpty();
            }
        };
    }

    public NewContactView(NewContactPresenter presenter) {
        this.presenter = presenter;

        setSpacing(true);
        setStyleName("marged");
        setWidth("95%");

        addComponent(contactName());
        addComponent(contactMail());
        addComponent(submitButton());
    }

    private Component submitButton() {
        Button submit = new Button("Submit");
        submit.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (!notEmptyStringValidator.isValid(name.getValue())) {
                    getWindow().showNotification("Name must not be empty");
                    return;
                }
                if (!notEmptyStringValidator.isValid(mail.getValue())) {
                    getWindow().showNotification("E-mail must not be empty");
                    return;
                }
                if (!emailValidator.isValid(mail.getValue())) {
                    getWindow().showNotification("Not a valid email");
                    return;
                }

                presenter.createContact(name.getValue().toString(), mail.getValue().toString());

            }
        });
        return submit;
    }

    private Component contactMail() {
        mail = new TextField("E-mail");
        mail.setRequired(true);
        mail.setRequiredError("Please enter a valid email");
        mail.addValidator(emailValidator);
        mail.addValidator(notEmptyStringValidator);
        mail.setValue("miso@java.sk");

        return mail;
    }

    private Component contactName() {
        name = new TextField("Name");
        name.setRequired(true);
        name.setRequiredError("Please enter a name");
        name.addValidator(notEmptyStringValidator);
        name.setValue("Miso");
        return name;
    }

}